<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
    <!--
      <el-form-item label="课程管理主键id" prop="courseManageId">
        <el-input
          v-model="queryParams.courseManageId"
          placeholder="请输入课程管理主键id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
    -->
      <el-form-item label="课程名称" prop="courseName">
        <el-input
          v-model="queryParams.courseName"
          placeholder="请输入课程名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教师名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入教师名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学期" prop="calendarPeriod">
        <el-input
          v-model="queryParams.calendarPeriod"
          placeholder="请输入学期"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教室名称" prop="classroomName">
        <el-input
          v-model="queryParams.courseName"
          placeholder="请输入教室名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['course_manage:course_manage:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['course_manage:course_manage:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['course_manage:course_manage:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['course_manage:course_manage:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="course_manageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="序号" type="index"align="center" width="50" prop/>
      <el-table-column label="课程编号" align="center" prop="courseManageId" />
      <!--
      <el-table-column label="课程管理主键id" align="center" prop="courseManageId" />
      <el-table-column label="课程id" align="center" prop="courseId" />
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="校历id" align="center" prop="calendarId" />
      <el-table-column label="教室id" align="center" prop="classroomId" />
      -->

      <el-table-column label="课程名称" align="center" prop="courseName" />
      <el-table-column label="教师名称" align="center" prop="userName" />
      <el-table-column label="学期名称" align="center" prop="calendarPeriod" />
      <el-table-column label="教室名称" align="center" prop="classroomName" />
      <el-table-column label="星期" align="center" prop="day" />
      <el-table-column label="课节" align="center" prop="courseManagePeriod" />


      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['course_manage:course_manage:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['course_manage:course_manage:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改course_manage下拉框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="course_manageRef" :model="form" :rules="rules" label-width="80px">


        <el-form-item @click="getCourseList" label="课程名称" prop="courseId" v-if="showCourseName">
          <el-select v-model="form.courseId">
            <el-option v-for="course in courseList" :key="course.id" :label="course.name" :value="course.courseName"></el-option>
          </el-select>

        </el-form-item>
        <el-form-item @click="getUserList" label="教师名称" prop="userId">
          <el-select v-model="form.userId">
            <el-option v-for="user in userList" :key="user.id" :label="user.name" :value="user.userName"></el-option>
          </el-select>

        </el-form-item>
        <el-form-item @click="getCalendarList" label="校历" prop="calendarId">
          <el-select v-model="form.calendarId">
            <el-option v-for="calendar in calendarList" :key="calendar.id" :label="calendar.period" :value="calendar.period"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item @click="getClassroomList" label="教室名称" prop="classroomId">
          <el-select v-model="form.classroomId">
            <el-option v-for="classroom in classroomList" :key="classroom.id" :label="classroom.name" :value="classroom.classroomName"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="星期" prop="day">
          <el-select v-model="form.day">
        <el-option label="星期一" value="星期一"></el-option>
        <el-option label="星期二" value="星期二"></el-option>
        <el-option label="星期三" value="星期三"></el-option>
        <el-option label="星期四" value="星期四"></el-option>
        <el-option label="星期五" value="星期五"></el-option>
        <el-option label="星期六" value="星期六"></el-option>
        <el-option label="星期日" value="星期日"></el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="课节" prop="courseManagePeriod">
          <el-select v-model="form.courseManagePeriod">
        <el-option label="第一节" value="1"></el-option>
        <el-option label="第二节" value="2"></el-option>
        <el-option label="第三节" value="3"></el-option>
        <el-option label="第四节" value="4"></el-option>
        <el-option label="第五节" value="5"></el-option>
        
          </el-select>
        </el-form-item>


      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Course_manage">
import { listCourse_manage, getCourse_manage, delCourse_manage, addCourse_manage, updateCourse_manage,listClassroom,listCalendar,listCourse,listUser } from "@/api/course_manage/course_manage";

const { proxy } = getCurrentInstance();

const course_manageList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const classroomList = ref([]);
const courseList = ref([]);
const calendarList = ref([]);
const userList = ref([]);


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    courseManageId: null,
    courseId: null,
    userId: null,
    calendarId: null,
    classroomId: null,
    courseName: null,
    userName: null,
    calendarPeriod: null,
    courseManagePeriod: null,
    classroomName: null,
    day: null
  },
  rules: {
    courseId: [
      { required: true, message: "课程名称不能为空", trigger: "blur" }
    ],
    userId: [
      { required: true, message: "教师名称不能为空", trigger: "blur" }
    ],
    calendarId: [
      { required: true, message: "学期不能为空", trigger: "blur" }
    ],
    classroomId: [
      { required: true, message: "教室名称不能为空", trigger: "blur" }
    ],
    day: [
      { required: true, message: "星期不能为空", trigger: "blur" }
    ],
    courseManagePeriod: [
      { required: true, message: "课节不能为空", trigger: "blur" }
    ]
  }
});


const { queryParams, form, rules } = toRefs(data);
// 新增一个标志位，用于区分添加和修改操作
const isUpdate = ref(false);
// 新增 showCourseName 用于控制课程名称输入框的显示与隐藏
const showCourseName = ref(true);





/** 查询教室列表 */
function getClassroomList() {
  loading.value = true;
  listClassroom(queryParams.value).then(response => {
    classroomList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  }).catch(console.log("15461"));
  console.log(classroomList.value)
}


/** 查询课程列表 */
function getCourseList() {
  loading.value = true;
  listCourse(queryParams.value).then(response => {
    courseList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  }).catch(console.log("15461"));
  console.log(courseList.value)
}


/** 查询学期列表 */
function getCalendarList() {
  loading.value = true;
  listCalendar(queryParams.value).then(response => {
    calendarList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  }).catch(console.log("15461"));
  console.log(calendarList.value)
}




/** 查询选课管理列表 */
function getList() {
  loading.value = true;
  listCourse_manage(queryParams.value).then(response => {
    course_manageList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 查询所有教师列表 */
function getUserList() {
  loading.value = true;
  console.log('请求参数:', queryParams.value); // 输出请求参数
  listUser(queryParams.value).then(response => {
    userList.value = response.rows;
    total.value = response.total;
    loading.value = false;
    console.log('userList 的值:', JSON.stringify(userList.value, null, 2)); 
  }).catch(error => {
    console.log("获取教师列表失败", error);
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    courseManageId: null,
    courseId: null,
    userId: null,
    calendarId: null,
    classroomId: null,
    courseName: null,
    userName: null,
    calendarPeriod: null,
    courseManagePeriod: null,
    classroomName: null,
    day: null
  };
  proxy.resetForm("course_manageRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.courseManageId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加排课";
  isUpdate.value = false;
  showCourseName.value = true;
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _courseManageId = row.courseManageId || ids.value
  getCourse_manage(_courseManageId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改course_manage";
    isUpdate.value = true;
    showCourseName.value = false;
  });
}

/** 提交按钮 */
function submitForm() {

//下拉框循环列表展示
  for (let i = 0; i <course_manageList.value.length; i++) {
    if(course_manageList.value[i].userName===form.value.userId){
      form.value.userId=course_manageList.value[i].userId
      break
    }
  }
for (let i = 0; i <classroomList.value.length; i++) {
    if(classroomList.value[i].classroomName===form.value.classroomId){
      form.value.classroomId=classroomList.value[i].classroomId
      break
    }
  }

  for (let i = 0; i <courseList.value.length; i++) {
    if(courseList.value[i].courseName===form.value.courseId){
      form.value.courseId=courseList.value[i].courseId
      break
    }
  }
  for (let i = 0; i <calendarList.value.length; i++) {
    if(calendarList.value[i].period===form.value.calendarId){
      form.value.calendarId=calendarList.value[i].calendarId
      break
    }
  }




  proxy.$refs["course_manageRef"].validate(valid => {
    if (valid) {
      if (form.value.courseManageId != null) {
        updateCourse_manage(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
          console.log('提交时form.value的值为:', form.value);
        addCourse_manage(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _courseManageIds = row.courseManageId || ids.value;
  proxy.$modal.confirm('是否确认删除course_manage编号为"' + _courseManageIds + '"的数据项？').then(function() {
    return delCourse_manage(_courseManageIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('course_manage/course_manage/export', {
    ...queryParams.value
  }, `course_manage_${new Date().getTime()}.xlsx`)
}

getList();
getClassroomList();
getCourseList();
getCalendarList();
getUserList();

</script>
